@echo off
chcp 65001
setlocal enabledelayedexpansion

:: Check if version parameter is provided
if "%~1"=="" (
    echo Error: Version number is required!
    echo Usage: build.bat [version]
    echo Example: build.bat 1.0.0
    exit /b 1
)

:: Set version number from parameter
set VERSION=%~1

:: Set image name
set IMAGE_NAME=servercheck
set REGISTRY=mydockerregistry.tommickey.cn/tommickey/servercheck

echo Starting build process...
echo Version: %VERSION%

:: 1. Build Go executable
echo Building Go executable...
go build -o %IMAGE_NAME%.exe
if %ERRORLEVEL% neq 0 (
    echo Failed to build Go executable!
    exit /b %ERRORLEVEL%
)

:: 2. Build Docker image
echo Building Docker image...
docker build -t %IMAGE_NAME%:latest .
if %ERRORLEVEL% neq 0 (
    echo Failed to build Docker image!
    exit /b %ERRORLEVEL%
)

:: 3. Tag image
echo Tagging image...
docker tag %IMAGE_NAME%:latest %REGISTRY%:%VERSION%
if %ERRORLEVEL% neq 0 (
    echo Failed to tag image!
    exit /b %ERRORLEVEL%
)

:: 4. Push image to private registry
echo Pushing image to private registry...
docker push %REGISTRY%:%VERSION%
if %ERRORLEVEL% neq 0 (
    echo Failed to push image!
    exit /b %ERRORLEVEL%
)

echo Build process completed!
echo Image pushed to: %REGISTRY%:%VERSION%

:: Clean up temporary files
echo Cleaning up temporary files...
del %IMAGE_NAME%.exe

endlocal 
